package com.qiudaozhang.service.impl;

import com.qiudaozhang.dto.ResponseCode;
import com.qiudaozhang.mapper.StgDAccountMapper;
import com.qiudaozhang.mapper.UserRechargeMapper;
import com.qiudaozhang.model.StgDAccount;
import com.qiudaozhang.model.User;
import com.qiudaozhang.model.UserRecharge;
import com.qiudaozhang.service.UserRechargeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class UserRechargeServiceImpl implements UserRechargeService {
    @Autowired
    private UserRechargeMapper userRechargeMapper;
    @Autowired
    private StgDAccountMapper stgDAccountMapper;

    @Override
    public String findNowCode() {
        return userRechargeMapper.findNowCode();
    }

    @Override
    public ResponseCode recharge(UserRecharge userRecharge, User user) {
        userRecharge.setBankAccount(user.getBankAccount());
        userRecharge.setBankName(user.getBankName());
        userRecharge.setCreditedMoney(userRecharge.getRechargeMoney());
        userRecharge.setCreditedTime(new Date());
        userRecharge.setUserId(user.getId().toString());
        ResponseCode responseCode = new ResponseCode();
        int row = userRechargeMapper.add(userRecharge);
        int row2 = stgDAccountMapper.recharge(userRecharge.getRechargeMoney(),user.getLoginCode());
        if (row > 0 && row2 > 0) {
            responseCode.setCode(ResponseCode.SUCCESS);
            responseCode.setMsg("充值成功！");
        }else{
            responseCode.setCode(ResponseCode.FAIL);
            responseCode.setMsg("充值失败！");
        }
        return responseCode;
    }
}
